using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;

[RequireComponent(typeof(CharacterInventory))]
[RequireComponent(typeof(NetworkIdentity))]
public class ItemStocker : NetworkBehaviour
{
	public string StockID = "mybox";

	public CharacterInventory inventory;

	private int updateTemp;

	private bool stockLoaded;

	private ObjectPlacing placing;

	public float DistanceLimit = 2f;

	public Vector3 Offset;

	public string ActiveText = "Interactive";

	private CharacterSystem characterTemp;

	[SyncVar(hook = "OnStockChanged")]
	public string DataText = string.Empty;

	public bool AddStarterItem;

	public bool DestroyIfEmpty;

	public float DestroyDelay = 1f;

	public string NetworkDataText
	{
		get
		{
			return DataText;
		}
		[param: In]
		set
		{
			ref string dataText = ref DataText;
			if (NetworkServer.localClientActive && !base.syncVarHookGuard)
			{
				base.syncVarHookGuard = true;
				OnStockChanged(value);
				base.syncVarHookGuard = false;
			}
			SetSyncVar(value, ref dataText, 1u);
		}
	}

	private void Start()
	{
		inventory = GetComponent<CharacterInventory>();
		placing = GetComponent<ObjectPlacing>();
		if ((bool)placing)
		{
			StockID = placing.ItemUID;
		}
		else
		{
			stockLoaded = true;
		}
		if (base.isServer)
		{
			LoadStock();
		}
		if (AddStarterItem && (bool)inventory)
		{
			inventory.SetupStarterItem();
		}
	}

	private void OnStockChanged(string datatext)
	{
		NetworkDataText = datatext;
		GetUpdateStock();
	}

	public void OpenStock()
	{
		GetUpdateStock();
	}

	private void Update()
	{
		if (inventory == null)
		{
			return;
		}
		if (base.isServer && updateTemp != inventory.UpdateCount && stockLoaded)
		{
			UpdateStock();
			SaveStock();
			updateTemp = inventory.UpdateCount;
		}
		if ((bool)characterTemp)
		{
			if (Vector3.Distance(base.transform.position, characterTemp.transform.position + Offset) > DistanceLimit)
			{
				OnExit();
			}
			else
			{
				OnStay();
			}
		}
		if (DestroyIfEmpty && inventory.Items.Count <= 0)
		{
			UnityEngine.Object.Destroy(base.gameObject, DestroyDelay);
		}
	}

	public void Pickup(GameObject character)
	{
		character.SendMessage("PickupStockCallback", this);
	}

	public void PickUpStock(GameObject characterObject)
	{
		CharacterSystem component = characterObject.GetComponent<CharacterSystem>();
		if ((bool)component && component.IsMine)
		{
			component.inventory.PeerTrade = inventory;
			OpenStock();
			UnitZ.Hud.OpenSecondInventory(inventory, "Stock");
		}
		characterTemp = component;
	}

	private void SaveStock()
	{
		if (!(inventory == null) && !(placing == null))
		{
			NetworkDataText = inventory.GetItemDataText();
			PlayerPrefs.SetString(StockID, DataText);
		}
	}

	private void LoadStock()
	{
		if (!(inventory == null) && !(placing == null))
		{
			if (PlayerPrefs.HasKey(StockID))
			{
				inventory.SetItemsFromText(PlayerPrefs.GetString(StockID));
				stockLoaded = true;
			}
			else
			{
				stockLoaded = true;
				SaveStock();
			}
		}
	}

	private void UpdateStock()
	{
		if (base.isServer)
		{
			NetworkDataText = inventory.GetItemDataText();
		}
	}

	public void GetUpdateStock()
	{
		inventory.SetItemsFromText(DataText);
	}

	public void OnStay()
	{
	}

	public void OnExit()
	{
		UnitZ.Hud.CloseSecondInventory();
		characterTemp.inventory.PeerTrade = null;
		characterTemp = null;
	}

	public void GetInfo()
	{
		UnitZ.Hud.ShowInfo(ActiveText, base.transform.position);
	}

	private void UNetVersion()
	{
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		if (forceAll)
		{
			writer.Write(DataText);
			return true;
		}
		bool flag = false;
		if ((base.syncVarDirtyBits & 1) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.Write(DataText);
		}
		if (!flag)
		{
			writer.WritePackedUInt32(base.syncVarDirtyBits);
		}
		return flag;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
		if (initialState)
		{
			DataText = reader.ReadString();
			return;
		}
		int num = (int)reader.ReadPackedUInt32();
		if ((num & 1) != 0)
		{
			OnStockChanged(reader.ReadString());
		}
	}
}
